//
//  MoreViewController.m
//  享动
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MoreViewController.h"
#import "LoginViewController.h"
#import "MoreView.h"
#import "UserViewController.h"
#import "UserModel.h"
@interface MoreViewController ()

@property (nonatomic, strong) MoreView *moreView;

@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _moreView = [[MoreView alloc] init];
    
    _moreView.naVc = self.navigationController;
    [self.view addSubview:_moreView];
    
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editDataAciton)];
    
    [rightButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName, nil] forState:UIControlStateNormal];
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    
    
    //self.toLoginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    // Do any additional setup after loading the view.
}

#pragma mark --- 点击登录button 触发的方法 ---
- (IBAction)toLogin:(id)sender
{
    LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:@"login" bundle:[NSBundle mainBundle]];
    [self presentViewController:loginVc animated:YES completion:nil];
}

#pragma mark --- 点击编辑个人资料按钮触发的方法---
- (void)editDataAciton
{
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"];

    if (isLogin == YES)
    {
        // 创建CATransition对象
        CATransition *animation = [CATransition animation];
        
        animation.duration = 0;
        
        //设置运动type
        animation.type = kCATransitionFromRight;
        
        //设置运动速度
        animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
        
        [self.navigationController.view.layer addAnimation:animation forKey:nil];
        
        UserViewController *userVc = [[UserViewController alloc] initWithNibName:@"User" bundle:[NSBundle mainBundle]];
        
        userVc.image = _moreView.model.avatar;
        
        [self.navigationController pushViewController:userVc animated:YES];
        
        __weak MoreViewController *blockSelf = self;
        
        userVc.userHeadImage = ^(UIImage *image){
            blockSelf.moreView.userHearImage.image = image;
        };
        userVc.userName = ^(NSString *name){
            blockSelf.moreView.userName.text = name;
        };

    }
    else
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该操作需要登录,是否立即登录" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:@"login" bundle:[NSBundle mainBundle]];
            [self presentViewController:loginVc animated:YES completion:nil];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:sure];
        [alert addAction:cancel];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self presentViewController:alert animated:YES completion:nil];
        });
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
